//
//  ST_DailyMomentSectionHeader.swift
//  btvideo
//
//  Created by mac on 2020/7/23.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit

class ST_DailyMomentSectionHeader: UITableViewHeaderFooterView {
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        
        self.contentView.backgroundColor = .white
        
        self.contentView.addSubview(btn0)
        btn0.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(5)
            make.width.equalTo(DailyMomentSectionButton.width)
            make.height.equalTo(DailyMomentSectionButton.height)
        }
        
        
        self.contentView.addSubview(btn1)
        btn1.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.left.equalTo(btn0.snp.right)
            make.width.equalTo(DailyMomentSectionButton.width)
            make.height.equalTo(DailyMomentSectionButton.height)
        }
        
        btn0.addTarget(self, action: #selector(click(sender:)), for: UIControl.Event.touchUpInside)
        
        btn1.addTarget(self, action: #selector(click(sender:)), for: UIControl.Event.touchUpInside)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var last_selected_btn = btn0
    
    fileprivate lazy var btn0:DailyMomentSectionButton = {
       let control = DailyMomentSectionButton()
        control.text = "热门"
        control.isSelected = true
        return control
    }()
    
    
    fileprivate lazy var btn1:DailyMomentSectionButton = {
       let control = DailyMomentSectionButton()
        control.text = "最新"
        return control
    }()
    
//    1:最新,2:推荐(默认)
    
    var selectClosure:((Int)->())?
    
    @objc fileprivate func click(sender:DailyMomentSectionButton){
        
        if sender.isSelected{
            return
        }
        
        sender.isSelected = !sender.isSelected
        last_selected_btn.isSelected = !last_selected_btn.isSelected
        last_selected_btn = sender
        
        selectClosure?(sender == btn0 ? 2 : 1)
        
    }
}


//MARK: - Center 样式
class DailyMomentSectionOfCenterHeader: UITableViewHeaderFooterView {
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        
        self.contentView.backgroundColor = .white
        
        self.contentView.addSubview(btn0)
        btn0.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.right.equalTo(self.snp.centerX)
            make.width.equalTo(36+60)
            make.height.equalTo(DailyMomentSectionButton.height)
        }
        
        
        self.contentView.addSubview(btn1)
        btn1.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.left.equalTo(btn0.snp.right)
            make.width.height.equalTo(btn0)
        }
        
        btn0.addTarget(self, action: #selector(click(sender:)), for: UIControl.Event.touchUpInside)
        
        btn1.addTarget(self, action: #selector(click(sender:)), for: UIControl.Event.touchUpInside)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var last_selected_btn = btn0
    
    fileprivate lazy var btn0:DailyMomentSectionButton = {
       let control = DailyMomentSectionButton()
        control.text = "热门"
        control.isSelected = true
        return control
    }()
    
    
    fileprivate lazy var btn1:DailyMomentSectionButton = {
       let control = DailyMomentSectionButton()
        control.text = "最新"
        return control
    }()
    
//    1:最新,2:推荐(默认)
    
    var selectClosure:((Int)->())?
    
    @objc fileprivate func click(sender:DailyMomentSectionButton){
        
        if sender.isSelected{
            return
        }
        
        sender.isSelected = !sender.isSelected
        last_selected_btn.isSelected = !last_selected_btn.isSelected
        last_selected_btn = sender
        
        selectClosure?(sender == btn0 ? 2 : 1)
        
    }
}


fileprivate class DailyMomentSectionButton:UIControl{
    
    static let width:CGFloat = 36+20
    
    static let height:CGFloat = 25+1+3+5
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-5-4)
            make.height.equalTo(25)
        }
        
        self.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.height.equalTo(3)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-5)
            make.width.equalTo(14)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate let normalColor = UIColor.init(hexString: "#565656")
    
    fileprivate let selectedColor = UIColor.init(hexString: "#333333")
    
    fileprivate let normalFont = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.regular)
    
    fileprivate let selected_font  = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.medium)
    
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.textColor = normalColor
        control.textAlignment = .center
        control.font = normalFont
        return control
    }()

    fileprivate lazy var line:UIView = {
        let control = UIView()
        control.backgroundColor = UIColor.init(hex: 0xFEC207)
        control.isHidden = true
        control.cornerRadius = 1
        return control
    }()
    
    override var isSelected: Bool{
        didSet{
            content_label.textColor = isSelected ? selectedColor : normalColor
            content_label.font = isSelected ? selected_font : normalFont
            
            line.isHidden = !isSelected
        }
    }
    
    var text:String?{
        didSet{
            content_label.text = text
        }
    }
    
}
